/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treenode.visitor;

import java.util.Stack;

/**
 *
 * @author Bartek
 */
public class EvaluateVisitor extends Visitor {

    private Stack<Integer> stack = new Stack<>();
    
    @Override
    public void visitNumericNode(NumericNode numericNode) {
        stack.push(numericNode.getValue());
    }

    @Override
    public void visitBinaryOperatorNode(BinaryOperatorNode binaryOperatorNode) {
        binaryOperatorNode.getLeft().accept(this);
        binaryOperatorNode.getRight().accept(this);
        
        Integer right = stack.pop();
        Integer left = stack.pop();
        
        stack.push(binaryOperatorNode.compute(left, right));
    }
    
    public Integer getValue() {
        return stack.pop();
    }
}
